package com.feib.soeasy.action;

import java.util.ArrayList;


import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationErrorHandler;
import net.sourceforge.stripes.validation.ValidationErrors;

import com.feib.soeasy.model.Group;
import com.feib.soeasy.model.UserLoginControl;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.GroupService;
import com.feib.soeasy.service.UserLoginControlService;
import com.feib.soeasy.util.UserUtil;

/**
 * @title (#)Soez9012ActionBean.java<br>
 * @description 強迫登出功能<br>
 * @author Anson Tsai<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/12/9 create by Anson Tsai<br>
 */
public class Soez9012ActionBean extends BaseActionBean implements ValidationErrorHandler {


    /**
     * 強迫登出功能畫面
     */
    private static final String DEFAULT_JSP = "/WEB-INF/views/soez9012/soez9012.jsp";

    /**
     * 強迫登出功能查詢清單畫面
     */
    private static final String USER_LIST_JSP = "/WEB-INF/views/soez9012/soez9012List.jsp";

    /**
     * 強迫登出功能查詢清單畫面
     */
    private static final String FORCE_LOGOUT_RESULT_JSP = "/WEB-INF/views/soez9012/forceLogoutResult.jsp";
    
    /**
     * 業者代號查詢畫面
     */
    private static final String AJAX_QUERY_GROUP_JSP = "/WEB-INF/views/soez9012/groupPopUpList.jsp";

    /**
     * 查詢條件-業者代號
     */
    @Validate(required = false, on = { "doAjaxQuery" })
    private String groupNo;

    /**
     * 查詢條件-使用者名稱
     */
    @Validate(required = false, on = { "doAjaxQuery" })
    private String userName;

    /**
     * 查詢結果
     */
    private PaginatedList<UserLoginControl> queryResult;

    /**
     * 鈎選的登出清單
     */
    private ArrayList<UserLoginControl> checkUserLogout;
    
    private boolean forceLogout = false;
    

    /**
     * 帳單業者查詢條件
     */
    private Group qryGroup;

    /**
     * 帳單業者查詢結果
     */
    private PaginatedList<Group> queryGroupResult;

    /**
     * 
     */
    @SpringBean("userLoginControlService")
    private UserLoginControlService userLoginControlService;
    
    @SpringBean("groupService")
    private GroupService groupService;
    

    @DontBind
    @DefaultHandler
    public Resolution doDefault() {
        logger.debug("Enter doDefault mehtod.");
        return new ForwardResolution(DEFAULT_JSP);
    }

    /**
     * 查詢
     * 
     * @return
     */
    public Resolution doAjaxQuery() {
        logger.debug("Enter doAjaxQuery mehtod.");
        try {
            
            if (! UserUtil.isBankGroup())
            {
                groupNo = UserUtil.getUserGroup().getGroupNo();
            }
            // 呼叫服務查詢
            queryResult = userLoginControlService.queryEnabledInfoByUserIdGroupNo(userName, groupNo, UserUtil.isBankGroup(), queryResult);

        }
        catch (Exception e) {
            logger.error("強迫登出功能查詢作業查詢錯誤(Soez9012ActionBean.doAjaxQuery):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQuery.exception"));
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }

        return new ForwardResolution(USER_LIST_JSP);
    }

    public Resolution doAjaxUserInfoDisabledAction() {
        logger.debug("Enter doAjaxUserInfoDisabledAction mehtod.");
        try {
            if (null == checkUserLogout) checkUserLogout = new ArrayList<UserLoginControl>();
           
            userLoginControlService.disableUserLoginInfo(checkUserLogout);
            
            forceLogout = true; 
            
        }
        catch (Exception e) {
            
            logger.error("強迫登出處理失敗.", e);
        }

        return new ForwardResolution(FORCE_LOGOUT_RESULT_JSP);
    }
    
    /**
     * 帳單業者查詢
     * @return
     */
    public Resolution doAjaxQueryGroups()
    {
        logger.debug("Enter doAjaxQueryGroups mehtod.");
        try {
            //如果不是銀行端，則查詢條件要加入帳單業者限制
            if (!UserUtil.isBankGroup())
            {
                throw new Exception("No Authority");
            }

            //USER未輸入查詢條件，所以查詢物件會是NULL(查詢物件不可為NULL)
            if (null == qryGroup)
                qryGroup = new Group();

            //呼叫服務查詢
            queryGroupResult = groupService.queryByGroupExcludeBank(qryGroup, queryGroupResult);
        }
        catch (Exception e) {
            logger.error("強迫登出功-帳單業者查詢錯誤(Soez9012ActionBean.doAjaxQueryGroups):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQuery.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }

        return new ForwardResolution(AJAX_QUERY_GROUP_JSP);
    }
    

    /**
     * @return 傳回 groupNo。
     */
    public String getGroupNo() {
        return groupNo;
    }

    /**
     * @param groupNo 要設定的 groupNo。
     */
    public void setGroupNo(String groupNo) {
        this.groupNo = groupNo;
    }

    /**
     * @return 傳回 userName。
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName 要設定的 userName。
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return 傳回 queryResult。
     */
    public PaginatedList<UserLoginControl> getQueryResult() {
        return queryResult;
    }

    /**
     * @param queryResult 要設定的 queryResult。
     */
    public void setQueryResult(PaginatedList<UserLoginControl> queryResult) {
        this.queryResult = queryResult;
    }

    /**
     * @return 傳回 checkUserLogout。
     */
    public ArrayList<UserLoginControl> getCheckUserLogout() {
        return checkUserLogout;
    }

    /**
     * @param checkUserLogout 要設定的 checkUserLogout。
     */
    public void setCheckUserLogout(ArrayList<UserLoginControl> checkUserLogout) {
        this.checkUserLogout = checkUserLogout;
    }

    
    /**
     * @return the qryGroup
     */
    public Group getQryGroup() {
        return qryGroup;
    }

    
    /**
     * @param qryGroup the qryGroup to set
     */
    public void setQryGroup(Group qryGroup) {
        this.qryGroup = qryGroup;
    }

    
    /**
     * @return the queryGroupResult
     */
    public PaginatedList<Group> getQueryGroupResult() {
        return queryGroupResult;
    }

    
    /**
     * @param queryGroupResult the queryGroupResult to set
     */
    public void setQueryGroupResult(PaginatedList<Group> queryGroupResult) {
        this.queryGroupResult = queryGroupResult;
    }

    
    /**
     * @return the forceLogout
     */
    public boolean isForceLogout() {
        return forceLogout;
    }

    
    /**
     * @param forceLogout the forceLogout to set
     */
    public void setForceLogout(boolean forceLogout) {
        this.forceLogout = forceLogout;
    }

}
